<?php

/**
 * Add a Services
 * 
 * @param string $service_name
 * @param int $service_duration
 * @param str $max_seats
 * @param int $min_seats
 * @param int $fill_seats_before
 * @param int $confirm_seats_before
 * @param int $confirm_seats_within
 * @param int $autoconfirm
 * @param string $form_shortcode
 * @param string $client_confirm_email
 * @param string $client_autoconfirm_email
 * @param string $client_cancelation_email
 * @param string $agent_confirm_email
 * @param string $agent_cancelation_email
 * @param string $admin_confirm_email
 * @param string $admin_cancelation_email
 * @param int $maxHost
 * @param string $capability
 * @return int value  on success, 0 on fail
 */
function web_apper_insert_services( $service_name, $service_duration, $max_seats, $min_seats, $fill_seats_before, $confirm_seats_before, $confirm_seats_within, $autoconfirm, $form_shortcode, $client_confirm_email, $client_autoconfirm_email, $client_cancelation_email, $agent_confirm_email, $agent_cancelation_email, $admin_confirm_email, $admin_cancelation_email, $maxHost, $capability) {	
	$service                  			= new WebApper\Service\Service;
	$service->service_name				= $service_name;
	$service->service_duration			= $service_duration;
	$service->max_seats					= $max_seats;
	$service->min_seats					= $min_seats;
	$service->fill_seats_before			= $fill_seats_before;
	$service->confirm_seats_before		= $confirm_seats_before;
	$service->confirm_seats_within		= $confirm_seats_within;
	$service->autoconfirm				= $autoconfirm;
	$service->form_shortcode 			= $form_shortcode;
	$service->client_confirm_email 		= $client_confirm_email;
	$service->client_autoconfirm_email 	= $client_autoconfirm_email;
	$service->client_cancelation_email 	= $client_cancelation_email;
	$service->agent_confirm_email		= $agent_confirm_email;
	$service->agent_cancelation_email	= $agent_cancelation_email;
	$service->admin_confirm_email 		= $admin_confirm_email;
	$service->admin_cancelation_email 	= $admin_cancelation_email;
	$service->maxHost					= $maxHost;
	$service->capability				= $capability;
		
	if ( $service->save() )
		return $service->service_id;

	return 0;
}

/**
 * Update a Services
 * 
 * @param int $service_id
 * @param string $service_name
 * @param int $service_duration
 * @param str $max_seats
 * @param int $min_seats
 * @param int $fill_seats_before
 * @param int $confirm_seats_before
 * @param int $confirm_seats_within
 * @param int $autoconfirm
 * @param string $form_shortcode
 * @param string $client_confirm_email
 * @param string $client_autoconfirm_email
 * @param string $client_cancelation_email
 * @param string $agent_confirm_email
 * @param string $agent_cancelation_email
 * @param string $admin_confirm_email
 * @param string $admin_cancelation_email
 * @param int $maxHost
 * @param string $capability
 * @return int value  on success, 0 on fail
 */
function web_apper_update_services( $service_id, $service_name, $service_duration, $max_seats, $min_seats, $fill_seats_before, $confirm_seats_before, $confirm_seats_within, $autoconfirm, $form_shortcode, $client_confirm_email, $client_autoconfirm_email, $client_cancelation_email, $agent_confirm_email, $agent_cancelation_email, $admin_confirm_email, $admin_cancelation_email, $maxHost, $capability) {	
	$service                   			= new WebApper\Service\Service($service_id);
	$service->service_name				= $service_name;
	$service->service_duration			= $service_duration;
	$service->max_seats					= $max_seats;
	$service->min_seats					= $min_seats;
	$service->fill_seats_before			= $fill_seats_before;
	$service->confirm_seats_before		= $confirm_seats_before;
	$service->confirm_seats_within		= $confirm_seats_within;
	$service->autoconfirm				= $autoconfirm;
	$service->form_shortcode 			= $form_shortcode;
	$service->client_confirm_email 		= $client_confirm_email;
	$service->client_autoconfirm_email 	= $client_autoconfirm_email;
	$service->client_cancelation_email 	= $client_cancelation_email;
	$service->agent_confirm_email		= $agent_confirm_email;
	$service->agent_cancelation_email	= $agent_cancelation_email;
	$service->admin_confirm_email 		= $admin_confirm_email;
	$service->admin_cancelation_email 	= $admin_cancelation_email;
	$service->maxHost					= $maxHost;
	$service->capability				= $capability;
	if ( $service->save() )
		return $service->service_id;

	return 0;
}
/**
 * Save Service Schedule
 *
 * @since WebApper (1.0)
 * @param int $service_id
 * @return 
 */
function web_apper_services_schedule( $service_id, $week_num, $startTime, $endTime ) {
	$service = new WebApper\Service\Service($service_id);
	$service->week_num = $week_num;
	$service->startTime = $startTime;
	$service->endTime = $endTime;
	return $service->save_schedule();
}

/**
 * Populate Service 
 *
 * @since WebApper (1.0)
 * @return 
 */
function web_apper_populate_service( $service_id) {
	$service = new WebApper\Service\Service($service_id);
	return $service;
}

/**
 * Archive a services
 *
 * @since WebApper (1.0)
 * @param int $service_id
 * @return 
 */
function web_apper_archive_services( $service_id ) {
	$service = new WebApper\Service\Service($service_id);
	return $service->archive();
}

/**
 * Get all staff user-id
 *
 */
function get_staff_users() {
	$blogusers = get_users('blog_id=1&orderby=nicename&role=staff');
	$staffUserId = array();
	foreach ($blogusers as $user){
		$staffUserId[] = $user->ID;
	}
	return $staffUserId;
}

/**
* takes two dates formatted as YYYY-MM-DD and creates an
* inclusive array of the dates between the from and to dates.
*/
function create_date_range_array($strDateFrom, $strDateTo){
	
	$aryRange=array();	
	$iDateFrom=mktime(1,0,0,substr($strDateFrom,5,2), substr($strDateFrom,8,2),substr($strDateFrom,0,4));
	$iDateTo=mktime(1,0,0,substr($strDateTo,5,2), substr($strDateTo,8,2),substr($strDateTo,0,4));	
	if ( $iDateTo >= $iDateFrom ){
		array_push( $aryRange, date('Y-m-d',$iDateFrom) ); // first entry
		while ($iDateFrom < $iDateTo){
				$iDateFrom+=86400; // add 24 hours
				array_push($aryRange,date('Y-m-d',$iDateFrom));
			}
	}
	return $aryRange;
}


/**
* DUPLICATE CAPABILITIES CHECKING
*/
function check_capabilities($capabilities)
{
	
}

/**
* Default client confirm email message
*/
function get_client_default_confirm_message()
{
	$msg = "Dear CLIENT_FIRST_NAME,

Your booking  information:
Name: CLIENT_FIRST_NAME CLIENT_LAST_NAME
Service Name: SERVICE_NAME
Booked To: STAFF_NAME
Booking Date-time: BOOKING_DATE START_TIME to END_TIME

CONFIRM_LINK

--The Team @ SITE_NAME";
	return $msg;
}

/**
* Default client auto confirm email message
*/
function get_client_default_auto_confirm_message()
{
	$msg = "Dear CLIENT_FIRST_NAME,

Your booking  information:
Name: CLIENT_FIRST_NAME CLIENT_LAST_NAME
Service Name: SERVICE_NAME
Booked To: STAFF_NAME
Booking Date-time: BOOKING_DATE START_TIME to END_TIME
			
--The Team @ SITE_NAME";
	return $msg;
}

/**
* Default client cancellation email message
*/
function get_client_default_cancellation_message()
{
	$msg = "Dear CLIENT_FIRST_NAME,

Your booking has been cancelled by an administrator.
			
--The Team @ SITE_NAME";
	return $msg;
}

/**
* Default staff confirm email message
*/
function get_staff_default_confirm_message()
{
	$msg = "Dear STAFF_NAME,

Client booking  information:
Client Name: CLIENT_FIRST_NAME CLIENT_LAST_NAME
Service Name: SERVICE_NAME
Booked To: STAFF_NAME
Booking Date-time: BOOKING_DATE START_TIME to END_TIME

Thanks!
--The Team @ SITE_NAME";
	return $msg;
}

/**
* Default client cancellation email message
*/
function get_staff_default_cancellation_message()
{
	$msg = "Dear STAFF_NAME,

One booking has been cancelled.
			
Thanks!
--The Team @ SITE_NAME";
	return $msg;
}

/**
* Default staff confirm email message
*/
function get_administrator_default_confirm_message()
{
	$msg = "Dear Admin,

Client booking  information:
Client Name: CLIENT_FIRST_NAME CLIENT_LAST_NAME
Service Name: SERVICE_NAME
Booked To: STAFF_NAME
Booking Date-time: BOOKING_DATE START_TIME to END_TIME

Thanks!
--The Team @ SITE_NAME";
	return $msg;
}

/**
* Default client cancellation email message
*/
function get_administrator_default_cancellation_message()
{
	$msg = "Dear Admin,

One booking has been cancelled .
			
Thanks!
--The Team @ SITE_NAME";
	return $msg;
}

/**
* Get month list
*/

function get_month_list(){
    $monthList = array();
	for($i=1;$i<13;$i++){
		$r = date("F",strtotime("2000-".$i."-01"));
		$monthList[date("F",strtotime("2000-".$i."-01"))]=constant($r);
	}
	for($i=1;$i<13;$i++){
		$r = date("M",strtotime("2000-".$i."-01"));
		$monthList[date("M",strtotime("2000-".$i."-01"))]=constant($r);
	}
	for($i=1;$i<8;$i++){
		$r = date("D",strtotime("22-01-2012 +$i days"));
		$monthList[date("D",strtotime("22-01-2012 +$i days"))]=constant($r);
	}
	for($i=1;$i<8;$i++){
		$r = date("l",strtotime("22-01-2012 +$i days"));
		$monthList[date("l",strtotime("22-01-2012 +$i days"))]=constant($r);
	}
	return $monthList;
}

/**
* Get short week list
*/
function getShortWeek($n) {
    $monthList = get_month_list();
    return strtr(date("D", strtotime("22-01-2012 +$n days")), $monthList);
}

/**
* Get week list
*/
function getWeek($n) {
    $monthList = get_month_list();
    return strtr(date("l", strtotime("22-01-2012 +$n days")), $monthList);
}

?>